﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;

namespace Job.Core
{
    /// <summary>
    /// 数据操作转化类
    /// </summary>
    public class DataHelper
    {
        public static DataTable[] DataReaderToDataTables(IDataReader vDataReader)
        {
            ArrayList TableArrayList = new ArrayList();

            do
            {
                DataTable NewDataTable = new DataTable();

                DataTable TableSchema = vDataReader.GetSchemaTable();

                for (int i = 1; i <= TableSchema.Rows.Count; i++)
                    NewDataTable.Columns.Add(TableSchema.Rows[i - 1]["ColumnName"].ToString(), System.Type.GetType(TableSchema.Rows[i - 1]["DataType"].ToString()));

                while (vDataReader.Read() == true)
                {
                    DataRow NewDataRow = NewDataTable.NewRow();

                    for (int i = 1; i <= vDataReader.FieldCount; i++)
                        NewDataRow[i - 1] = vDataReader[i - 1];

                    NewDataTable.Rows.Add(NewDataRow);
                }

                TableArrayList.Add(NewDataTable);

            } while (vDataReader.NextResult() == true);

            DataTable[] ReturnDataTable = new DataTable[TableArrayList.Count];

            for (int i = 1; i <= TableArrayList.Count; i++)
                ReturnDataTable[i - 1] = (DataTable)TableArrayList[i - 1];

            return ReturnDataTable;
        }
    }
}
